package com.tangkun.environment.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.application.Session;
import com.tangkun.environment.entity.token.LoginReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改昵称
 */

public class ModifyNickNameActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.et_modify)
    MaterialEditText etModify;
    @BindView(R.id.iv_modify)
    ImageView ivModify;
    @BindView(R.id.btn_modify)
    Button btnModify;

    private Context mContext;
    /**
     * 群名称
     */
    private String content = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        content = getIntent().getStringExtra("content");
        setContentView(R.layout.activity_modify);
        ButterKnife.bind(this);
        title.setText("修改昵称");
        //历史信息
        if (TextUtils.isEmpty(content)) {
            etModify.setText("");
            ivModify.setVisibility(View.GONE);
        } else {
            etModify.setText(content + "");
            etModify.setSelection(content.length());//光标在末尾
            ivModify.setVisibility(View.VISIBLE);
        }
        initListener();
    }

    private void initListener() {
        //监听输入框文字变化，显示与隐藏清空按钮
        etModify.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etModify.getText().toString().length() > 0) {
                    ivModify.setVisibility(View.VISIBLE);
                } else {
                    ivModify.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick({R.id.back, R.id.iv_modify, R.id.btn_modify})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回按钮
                Utils.hideSoftKeyboard(ModifyNickNameActivity.this, back);
                JumpHelper.finish(mContext);
                break;
            case R.id.iv_modify://清空
                etModify.setText("");
                break;
            case R.id.btn_modify://确定按钮
                updateRemark();
                break;
        }
    }

    /**
     * 修改接口
     */
    private void updateRemark() {
        final String content = etModify.getText().toString().trim();
        //更新用户头像
        Map<String, String> map = new HashMap<String, String>();
        map.put("nickname", content);
        fromNetwork.updateUser(map).compose(new RxActivityHelper<LoginReturn>().ioMain(ModifyNickNameActivity.this, true))
                .subscribe(new MySubscribe<LoginReturn>(mContext, true) {
                    @Override
                    protected void success(final LoginReturn resultBean) {
                        if (resultBean.data != null) {
                            showToast("修改成功");
                            //更新用户信息
                            Session.getInstance().getUserInfo().nickname = content;
                            Utils.hideSoftKeyboard(ModifyNickNameActivity.this, back);
                            JumpHelper.finish(mContext);
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }
}